<?php


namespace app\xiangji\controller;


use app\Request;
use app\xiangji\BaseController;
use app\common\helpers\Response;
use app\xiangji\services\UserService;
use think\App;
use think\exception\ValidateException;
use app\common\validates\Auth as AuthValidate;

class Auth extends BaseController
{
    private $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function wxLogin(Request $request)
    {
        //校验数据
        try {
            validate(AuthValidate::class)->scene('login')->check($request->post(['code', 'iv', 'encryptedData']));
            $result = $this->userService->wxLogin($request);
            return $this->success($result);
        }catch (ValidateException $exception){
            return $this->failed($exception->getError(), Response::HTTP_BAD_REQUEST);
        }
    }

    public function logout()
    {
        $result = $this->userService->logout();
        return $this->success($result);
    }

    public function me()
    {
        $result = $this->userService->me();
        return $this->success($result);
    }
}
